/**
 * build_project
 * <p>
 * Copyright 2014 cayden.cn, Inc. All rights reserved.
 *
 * @author Administrator
 * @date 2021-07-01 09:20
 */
package com.cayden.project.componets.mybatis;

import com.cayden.config.boot.Apps;
import org.mybatis.spring.mapper.ClassPathMapperScanner;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Collection;
import java.util.Map;

/**
 * @author cayden
 * @date 2021-07-01 09:20
 */
public class MapperScannerRegister implements
        BeanFactoryAware, ImportBeanDefinitionRegistrar, ResourceLoaderAware {

    private BeanFactory beanFactory;

    private ResourceLoader resourceLoader;

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        MybatisProperties mybatisProperties = Apps.buildProperties(MybatisProperties.class);

        // 是否支持多数据源
        if (!mybatisProperties.isSupportMultiDataSource()) {
            ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
            try {
                if (this.resourceLoader != null) {
                    scanner.setResourceLoader(this.resourceLoader);
                }
                scanner.setMarkerInterface(EntityMybatisDao.class);
                scanner.registerFilters();
                Collection<String> daoScanPackages = mybatisProperties.getDaoScanPackages().values();
                scanner.doScan(daoScanPackages.toArray(new String[daoScanPackages.size()]));
            } catch (IllegalStateException ex) {
            }
            return;
        }

        // 不支持多数据源
        for (Map.Entry<String, MybatisProperties.Multi> entry :
                mybatisProperties.getMulti().entrySet()) {
            ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
            try {
                if (this.resourceLoader != null) {
                    scanner.setResourceLoader(this.resourceLoader);
                }
                scanner.setMarkerInterface(EntityMybatisDao.class);
                if (entry.getValue().isPrimary()) {
                    scanner.setSqlSessionFactoryBeanName("sqlSessionFactory");
                } else {
                    scanner.setSqlSessionFactoryBeanName(entry.getKey() + "SqlSessionFactory");
                }
                scanner.registerFilters();
                scanner.doScan(entry.getValue().getScanPackage());
            } catch (IllegalStateException ex) {
            }
        }

    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

}
